package org.mysfit.ctp4j.api.controller.v1;

import org.mysfit.ctp4j.api.vo.OrderInsertVO;
import org.mysfit.ctp4j.api.vo.QuoteInsertVO;
import org.mysfit.ctp4j.api.vo.QuoteVO;
import org.mysfit.ctp4j.core.service.CommonService;
import org.mysfit.ctp4j.core.service.PolyService;
import org.mysfit.ctp4j.core.service.TradeService;
import org.mysfit.ctp4j.core.service.impl.TradeHelper;
import org.mysfit.ctp4j.infra.pojo.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * @author long.gf
 * @since 2022-12-06
 *
 */
@RestController
@RequestMapping("/v1/trade")
public class TradeController {

	private static final Logger LOGGER = LoggerFactory.getLogger(TradeController.class);

	@Autowired
	private TradeService service;
	
	@Autowired
	private PolyService polyService;

	@Autowired
	private CommonService commonService;
	
	@PostMapping("/orderInert/{investorId}")
	public ApiResponse<String> orderInert(@PathVariable("investorId")String investorId,
			@RequestBody OrderInsertVO params) {
		try {
			service.doOrder(investorId, params);
			return ApiResponse.ok();
		} catch (Exception e) {
			return ApiResponse.error(e);
		}
	}
	
	@PostMapping("/quoteInert/{investorId}")
	public ApiResponse<String> quoteInert(@PathVariable("investorId")String investorId,
			@RequestBody QuoteInsertVO params) {
		try {
			service.doQuote(investorId, params);
			return ApiResponse.ok();
		} catch (Exception e) {
			return ApiResponse.error(e);
		}
	}
	
	@GetMapping("/initTrade/{investorId}")
	public ApiResponse<String> quoteInert(@PathVariable("investorId")String investorId) {
		new Thread(() -> {
			try {
				TradeHelper.getInstance(investorId,polyService,commonService).initTradeApi(investorId);
			} catch (Exception e) {
				LOGGER.error("接口初始化异常",e);
 			}
		}).start();
		return ApiResponse.ok();
	}
	
	@PostMapping("/doQuote/{investorId}")
	public ApiResponse<String> doQuote(@PathVariable("investorId")String investorId,@RequestBody QuoteVO q) {
		new Thread(() -> {
			try {
				TradeHelper.getInstance(investorId,polyService,commonService).doCtpOrder(q);
			} catch (Exception e) {
				LOGGER.error("quteo异常",e);
 			}
		}).start();
		return ApiResponse.ok();
	}
	
	@PostMapping("/doTestQuote/{investorId}")
	public ApiResponse<String> doTestQuote(@PathVariable("investorId")String investorId,@RequestBody QuoteVO q) {
		new Thread(() -> {
			try {
				TradeHelper.getInstance(investorId,polyService,commonService).doTestCtpOrder(q);
			} catch (Exception e) {
				LOGGER.error("quteo异常",e);
 			}
		}).start();
		return ApiResponse.ok();
	}
	
}
